import Vue from "vue"
var eventHub = new Vue({
    data:function(){
        return {
            todos:[],
            todosName:[],
            money : 0,
            numAll : [],
            isShow:true
        }
    },
    created : function(){
        this.$on("add", this.addTodo);
        this.$on("minus",this.deleteTodo);
    },
    beforeDestroy : function () {
        this.$off('add',this.addTodo);
        this.$off("addTwo",this.addTodoTwo);
        this.$off("minus",this.deleteTodo);
    },
    methods:{
        addTodo : function (m) {
            this.money+=m.price*1;
            var a = this.todosName.indexOf(m.main_name);
            this.numAll.push(m);
            if(a == -1){
                m.count = 1;
                this.todos.push(m);
                this.todosName.push(m.main_name);
            }else{
                this.todos[a].count++;
            }
            this.isShow = false
        },
        deleteTodo : function (x) {
            this.money-=x.price*1;
            x.count--;
            this.numAll.splice(x,1);
            var b = this.todosName.indexOf(x.main_name);
            if(x.count == 0){
                this.todos.splice(b,1);
                this.todosName.splice(b,1)
            }
        }
    }
});
export default eventHub;
